//index.js
//获取应用实例

var app = getApp()
Page({ 
  data: {
    motto: 'Hello World',
    userInfo: {},
    serviceItem: [],
     show_nav: "h_nav"

  },
  onLoad: function (e) { 
    var that = this;
      wx.showLoading({
        title: '加载中',
      });
       that.setData({
      userInfo: wx.getStorageSync("userInfo"),
    })
       var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2]; 
                  prevPage.setData({
                    show_nav: 'h_nav'
                  });

      wx.request({
        url: app.globalData.api_url + "index.php?ac=hapi&ts=item_list",
        data: {ism:1},
        header: { 'content-type': 'application/json' },
        success: function (res) {
          wx.hideLoading();
          that.setData({ 
            itemList: res.data.list
          })
          console.log(res.data);
        }

      })
      , setTimeout(function () {
        wx.hideLoading()
      }, 30000)
  },

   show_nav: function (e) {

          var that = this;
          that.setData({
            show_nav: 'Haircut_nav'
          });
  },
  serviceItemAdd:function(e){
    console.log(e.detail.value);
    var that = this; 
      wx.request({
        url: app.globalData.api_url + "index.php?ac=hapi&ts=item_add",
        data: { 
           item_name:e.detail.value.item_name,
           item_time:e.detail.value.item_time
         },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          if( res.data.result == 0 ){
            wx.showToast({
              title: '添加失败!',
              icon: 'loading',
              duration: 2000
            })
          }else{
            wx.showToast({
              title: '添加成功!',
              icon: 'success',
              duration: 2000
            });
            that.setData({
              itemList: res.data.list
            }); 
          };
          
          setTimeout(function(){
              wx.hideToast()
          },5000);
          console.log(res.data);
          console.log(e.detail.value.item_name);
          console.log(e.detail.value.item_time);
        }

      })
    
  },
  editItem: function (e) {
    console.log(e.currentTarget.dataset);
    var that = this;
    wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=personal_item_update",
      data: {
        hid:1,
        item_id: e.currentTarget.dataset.itemId,
        item_name: e.currentTarget.dataset.item_name,
        item_time: e.currentTarget.dataset.item_time
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        if (res.data.result == 0) {
          wx.showToast({
            title: '操作失败!',
            icon: 'loading',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '操作成功!',
            icon: 'success',
            duration: 2000
          });
          that.setData({
            itemList: res.data.list
          });
        };

        setTimeout(function () {
          wx.hideToast()
        }, 5000);
        console.log(res.data); 
      }

    })

  }

})
